from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy

from flask_login import LoginManager

#from .flaskAdmin import flaskAdmin
from flask_admin import Admin,BaseView ,expose
from flask_admin.contrib.sqla import ModelView

app = Flask(__name__)
    #获取配置文件1
app.config.from_object('config')
    #获取配置文件2
    #app.config.from_pyfile('config')

db = SQLAlchemy(app)
#flaskAdmin.init_app(app)
flaskAdmin = Admin(app, name='microblog', template_mode='bootstrap3')

#登录
login_manager = LoginManager()
login_manager.session_protection = 'strong'
login_manager.login_view='login'



#def create_app():


    #db.init_app(app)
    #login_manager.init_app(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    username = db.Column(db.String(64),nullable = True,index = True,unique = True)
    email = db.Column(db.String(120), index = True, unique = True)
    password_hash = db.Column(db.String(128))
    role_id = db.Column(db.SmallInteger)

class MyView(ModelView):
    can_delete = False  # disable model deletion

flaskAdmin.add_view(MyView(User, db.session))

class AnalyticsView(BaseView):
    @expose('/own')
    def index(self):
        return self.render('admin/analytics_index.html')

flaskAdmin.add_view(AnalyticsView(name='Analytics', endpoint='analytics'))


#蓝图
from .auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint)

from .blog import blog as blog_blueprint
app.register_blueprint(blog_blueprint)

from .common import common as common_blueprint
app.register_blueprint(common_blueprint)

#from .admin import admin as admin_blueprint
#app.register_blueprint(admin_blueprint)

from .api import api as api_blueprint
app.register_blueprint(api_blueprint)

from .oauth2Server import oauth2Server as oauth2Server_blueprint
app.register_blueprint(oauth2Server_blueprint)

from .oauth2Client import oauth2Client as oauth2Client_blueprint
app.register_blueprint(oauth2Client_blueprint)

#from .flaskAdmin import flaskAdmin as flaskAdmin_blueprint
#app.register_blueprint(flaskAdmin_blueprint)


from . import views, models
    #return app
